package com.matrix.index.ui.home

import android.animation.Animator
import android.view.View
import android.view.View.VISIBLE
import android.view.ViewAnimationUtils
import androidx.activity.enableEdgeToEdge
import androidx.core.view.isGone
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.utils.TextUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.matrix.basecore.arouter.ARouterUtils
import com.matrix.basecore.base.BaseActivity
import com.matrix.basecore.constant.Constant
import com.matrix.index.databinding.ActivitySearchBinding
import com.matrix.index.ui.home.adapter.SearchAdapter
import com.matrix.index.ui.home.bean.SearchBean
import org.litepal.LitePal
import org.litepal.extension.find
import kotlin.math.max

@Route(path = Constant.ARouterPath.SearchActivityPath)
class SearchActivity : BaseActivity<ActivitySearchBinding>() {

    private var searchBeans: MutableList<SearchBean> = mutableListOf()
    private lateinit var adapter: SearchAdapter
    private var isAnim = false
    override fun getViewBinding(): ActivitySearchBinding {
        enableEdgeToEdge()
        return ActivitySearchBinding.inflate(layoutInflater)
    }

    override fun initView() {
        var llm = LinearLayoutManager(this)
        adapter = SearchAdapter(searchBeans.toMutableList())
        mViewBinding.recyclerview.layoutManager = llm
        mViewBinding.recyclerview.adapter = adapter
    }

    override fun initData() {

    }

    override fun initListener() {
        mViewBinding.ivBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        mViewBinding.main.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        mViewBinding.tvSearch.setOnClickListener {
            LogUtils.i(mViewBinding.llPreview.isGone)
            if (mViewBinding.llPreview.isGone) {
                mViewBinding.llPreview.visibility = VISIBLE
                startAnimation(mViewBinding.llPreview)
            }
            searchBeans.clear()
            adapter.setList(searchBeans)
            //加载数据
            var key = mViewBinding.etSearch.text.trim()
            searchBeans = if (!TextUtils.isEmpty(key)) {
                LitePal.where("jokeDesc like ? or imageDesc like ?", "%${key}%", "%${key}%")
//                    .order("timestamp desc")
                    .limit(80)
                    .find<SearchBean>().toMutableList()
            } else {
                LitePal.limit(100).find<SearchBean>().toMutableList()
            }
            if (searchBeans.size == 0) {
                ToastUtils.showShort("未搜索到任何数据")
            }
            searchBeans.shuffle()
            adapter.setList(searchBeans)
            KeyboardUtils.hideSoftInput(this)
        }
        adapter.setOnItemClickListener { _, _, position ->
            run {
                var search = searchBeans[position]
                if (search.itemType == SearchBean.JOKE) {
                    ARouterUtils.startJokePreviewActivity(search.jokeDesc)
                }
                if (search.itemType == SearchBean.IMAGE || search.itemType == SearchBean.WALL) {
                    ARouterUtils.startPreviewActivityPath(
                        transitionData(search.imageUrl),
                        0
                    )
                }
            }
        }
    }

    private fun transitionData(desc: String): ArrayList<String> {
        var descs: ArrayList<String> = arrayListOf()
        descs.add(desc)
        return descs
    }

    private fun startAnimation(view: View) {
        view.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
            if (!isAnim) {
                val animationTop = ViewAnimationUtils.createCircularReveal(
                    view, 0,
                    0, 0f,
                    max(
                        (view.width).toDouble(),
                        (view.height).toDouble()
                    ).toFloat()
                )
                animationTop.setDuration(1000)
                animationTop.addListener(object : Animator.AnimatorListener {
                    override fun onAnimationStart(p0: Animator) {

                    }

                    override fun onAnimationEnd(p0: Animator) {
                        isAnim = true
                    }

                    override fun onAnimationCancel(p0: Animator) {

                    }

                    override fun onAnimationRepeat(p0: Animator) {

                    }
                })
                animationTop.start()
            }
        }
    }
}